package com.hksj.wmp.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.acm.dto.HistoryCurveDTO;
import com.hksj.acm.service.DeviceService;
import com.hksj.acm.vo.DeviceAttributeVO;
import com.hksj.acm.vo.HistoryCurveVO;
import com.hksj.base.entity.Properties;
import com.hksj.base.query.*;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.ChartVO;
import com.hksj.base.vo.DeviceOverviewVO;
import com.hksj.base.vo.DeviceStatusVO;
import com.hksj.cm.service.DeviceListService;
import com.hksj.cm.vo.ParticleValueHistoryVO;
import com.hksj.common.core.model.Rest;
import com.hksj.crsm.service.CrsmService;
import com.hksj.crsm.vo.BarChartVO;
import com.hksj.gas.dto.GasDeviceDTO;
import com.hksj.gas.dto.GasDeviceDetailsDTO;
import com.hksj.gas.service.GasDeviceService;
import com.hksj.gas.vo.GasDeviceTimeVO;
import com.hksj.gas.vo.GasDeviceVO;
import com.hksj.wmp.service.WeChatDeviceService;
import com.hksj.wmp.vo.DeviceRunStatusVO;
import com.hksj.wmp.vo.JJDLevelSituationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zengke
 * @version 1.0
 * @see WeChatDeviceController
 * @since 2024/10/14 17:54
 */

@Api(tags = "微信小程序-设备列表管理")
@RequestMapping("/api/wx/wechatDevice")
@RestController
public class WeChatDeviceController {

    @Autowired
    private WeChatDeviceService wechatDeviceService;

    @Autowired
    private DefaultDeviceService defaultDeviceService;

    @Autowired
    private GasDeviceService gasDeviceService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceListService deviceListService;

    @Autowired
    private CrsmService crsmService;

    @ApiOperation(value = "设备首页-查询设备总体情况")
    @PostMapping("/queryDeviceOverallStatus")
    public Rest<Map<String,String>> queryDeviceOverallStatus(@RequestBody BaseSpecialityQuery query){
        Map<String, String> deviceOverview = wechatDeviceService.queryDeviceOverallStatus(query);
        return Rest.ok(deviceOverview);
    }


    @ApiOperation(value = "设备明细-查询设备概览")
    @PostMapping("/queryDeviceOverview")
    public Rest<DeviceOverviewVO> queryDeviceOverview(@RequestBody @Validated DeviceOverviewQuery query){
        DeviceOverviewVO deviceOverview = defaultDeviceService.getDeviceOverview(query);
        return Rest.ok(deviceOverview);
    }


    @ApiOperation(value = "设备明细-查询机房安全漏水柱状图")
    @PostMapping("/queryJFAQLousDeviceHistoryBar")
    public Rest<List<BarChartVO>> queryJFAQLousDeviceHistoryBar(@RequestBody @Validated ChartQuery query) {
        List<BarChartVO> list = crsmService.listBarChartData(query);
        return Rest.ok(list);
    }

    @ApiOperation(value = "设备明细-查询洁净度等级情况")
    @PostMapping("/queryJJDLevelSituation")
    public Rest<JJDLevelSituationVO> queryJJDLevelSituation(@RequestBody BaseSpecialityQuery query){
        JJDLevelSituationVO jjdLevelSituationVO = wechatDeviceService.queryJJDLevelSituation(query);
        return Rest.ok(jjdLevelSituationVO);
    }

    @ApiOperation(value = "设备明细-查询洁净度设备历史详情",response = ParticleValueHistoryVO.class)
    @PostMapping("/queryJJDParticleValueHistory")
    public Rest<Page<ParticleValueHistoryVO>> queryJJDParticleValueHistory(
            @ApiParam(value = "起始页", required = false, defaultValue = "1") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", required = false, defaultValue = "10") @RequestParam(required = false, defaultValue = "15") Integer pageSize,
            @ApiParam(value = "设备id", required = true) @RequestParam(required = true) String deviceId,
            @ApiParam(value = "时间区间", required = true, defaultValue = "0") @RequestParam(required = true, defaultValue = "0") int section){
        Page<ParticleValueHistoryVO> page = deviceListService.eqParticleValueHistoryList(pageNum, pageSize, deviceId, section);
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询洁净度设备折线图")
    @PostMapping("/queryJJDParticleCurve")
    public Rest<com.hksj.cm.vo.DeviceVO> queryJJDParticleCurve(
            @ApiParam(value = "设备id", required = true) @RequestParam(required = true) String deviceId,
            @ApiParam(value = "时间区间", required = true, defaultValue = "0") @RequestParam(required = true, defaultValue = "0") int section){
        com.hksj.cm.vo.DeviceVO deviceVO = deviceListService.eqParticleValueHistory(deviceId, section);
        return Rest.ok(deviceVO);
    }

    @ApiOperation(value = "设备明细-查询空调机组设备启停情况")
    @PostMapping("/queryDeviceRunStatus")
    public Rest<DeviceRunStatusVO> queryDeviceRunStatus(@RequestBody BaseSpecialityQuery query){
        DeviceRunStatusVO deviceRunStatusVO = wechatDeviceService.queryDeviceRunStatus(query);
        return Rest.ok(deviceRunStatusVO);
    }

    @ApiOperation(value = "设备明细-查询空调机组的设备参数详情")
    @GetMapping("/queryKTJZDeviceDetailList/{deviceId}")
    public Rest<List<DeviceAttributeVO>> queryKTJZDeviceDetailList(@PathVariable("deviceId") String deviceId){
        List<DeviceAttributeVO> deviceList = deviceService.monitorDataByDeviceId(deviceId);
        return Rest.ok(deviceList);
    }

    @ApiOperation(value = "设备明细-查询空调机组的设备历史曲线")
    @PostMapping("/queryKTJZDeviceHistoryCurve")
    public Rest<List<HistoryCurveVO>> queryKTJZDeviceHistoryCurve(@RequestBody @Valid HistoryCurveDTO dto){
        List<HistoryCurveVO> historyCurve = deviceService.queryHistoryCurve(dto);
        return Rest.ok(historyCurve);
    }

    @ApiOperation(value = "设备明细-查询医气压力监测设备列表",response = GasDeviceVO.class)
    @PostMapping("/queryYQPressureDeviceListByPaging")
    public Rest<IPage<GasDeviceVO>> queryGasPressureDeviceListByPaging(@RequestBody GasDeviceDTO dto){
        IPage<GasDeviceVO> page = gasDeviceService.queryPressureGasDeviceListByPage(dto);
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询医气压力监测设备详情")
    @PostMapping("/queryYQPressureDeviceDetailsList")
    public Rest<IPage<GasDeviceTimeVO>> queryYQPressureDeviceDetailsList(@RequestBody GasDeviceDetailsDTO dto){
        IPage<GasDeviceTimeVO> page = gasDeviceService.queryDeviceDetailsList(dto);
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询医气流量设备列表")
    @PostMapping("/queryYQFlowDeviceListByPaging")
    public Rest<IPage<GasDeviceVO>> queryGasFlowDeviceListByPaging(@RequestBody GasDeviceDTO dto){
        IPage<GasDeviceVO> page = gasDeviceService.queryFlowGasDeviceListByPage(dto);
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询医气流量设备详情")
    @PostMapping("/queryYQFlowDeviceList")
    public Rest<IPage<GasDeviceTimeVO>> queryYQFlowDeviceList(@RequestBody GasDeviceDetailsDTO dto){
        IPage<GasDeviceTimeVO> page = gasDeviceService.queryFlowDeviceList(dto);
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询设备列表",response = DeviceStatusVO.class)
    @PostMapping("/queryDeviceListByPaging")
    public Rest<Page<DeviceStatusVO>> queryDeviceListByPaging(@RequestBody @Validated BaseDeviceQuery query){
        Page<DeviceStatusVO> page;
        if (CollUtil.isNotEmpty(query.getCode()) && query.getCode().contains("KTJZ")){
            page = wechatDeviceService.queryKTJZDeviceList(query);
        } else {
            page = defaultDeviceService.pageDevice(query);
        }
        return Rest.ok(page);
    }

    @ApiOperation(value = "设备明细-查询设备历史曲线")
    @PostMapping("/queryDeviceHistoryCurve")
    public Rest<List<ChartVO>> queryDeviceHistoryCurve(@RequestBody @Validated ChartQuery query) {
        List<ChartVO> list = defaultDeviceService.listChartData(query);
        return Rest.ok(list);
    }


    @ApiOperation(value = "设备明细-查询设备历史记录",response = Properties.class)
    @PostMapping("/queryDeviceHistoryRecord")
    public Rest<Page<Properties>> queryDeviceHistoryRecord(@RequestBody @Validated BaseRecordQuery query){
        Page<Properties> page = defaultDeviceService.pageRecord(query);
        return Rest.ok(page);
    }

}
